package CS4310.csueb.visual;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/////////////////////////// class sprite ////////////////////////
public abstract class Sprite extends JLabel {

	private int x; // sprite's x coordinate
	protected int dx; // change in x
	private int y; // srite's y coordinate
	protected int dy; // change in y
	private int height; // the height of the sprite
	private int width; // the width of the sprite
	
	//in the future I plan to have multiple images
	//currently there is one image loaded
	private BufferedImage heroSprites[];
	
	private BufferedImage img;			//temp to hold images while loading
	String filename;
	protected Rectangle bound; // the binding box for the sprite
	private static final long serialVersionUID = 5760187134394967932L;

	// *********** default constructor ***********/
	public Sprite() {
		setLayout(null); // custom layout
		setDoubleBuffered(true);
		heroSprites = new BufferedImage[4];
	}

	// ************* constructor ****************/
	// may not need to place or draw yet
	public Sprite(String pfilename) {
		this();
		filename = pfilename;
		loadSpriteImg();
	}

	// ************* constructor ****************/
	public Sprite(String pfilename, int px, int py) {
		this();
		x = px;
		y = py;
		filename = pfilename;
		bound = new Rectangle(x-2, y-2, width+4, width+4);
		loadSpriteImg();
	}

	// ************* loadSpriteImg() **************/
	private void loadSpriteImg() {
		// reads in image file to BufferedImage
		try {
			img = ImageIO.read(new File(filename));
		} catch (IOException e) {
		}
		width = img.getWidth();
		height = img.getHeight();
		setBounds(x, y, width, height);
		
		setIcon(new ImageIcon(img));
	}

	// ************** getImg() **************/
	// returns the img to paint
	public BufferedImage getImg() {
		return img;
	}

	// ************ move() ***************//
	public void move() {
		x = x + dx;
		y = y + dy;
		bound.setLocation(x, y);
 
	}

	
	// *********** collide(sprite) ****/
 	public boolean collide(Sprite other) {
 		return this.bound.intersects(other.bound);
 
 	}
	
	
	// *********** setXandY() *************//
	public void setXandY(int px, int py) {
		x = px;
		y = py;
	}
	
	//************ setDX ***************//
	public void setDX(int pDX){
		dx = pDX;
	}

	//************ setDY ***************//
	public void setDY(int pDY){
		dy = pDY;
	}
	
	
	
	// ************ getx() *****************/
	// returns the x coordinate of the sprite
	public int getx() {
		return x;
	}

	// ************ gety() *****************/
	// returns the y coordinate of the sprite
	public int gety() {
		return y;
	}

	// ************ getWidth() *************/
	public int getWidth() {
		return width;
	}

	// ************ getHeight() *************/
	public int getHeight() {
		return height;
	}

	// ********* override paintComponent() *********//
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;

		// draws the whole image "img" at accessed coordinates
		g2d.drawImage(img, x, y, this);
	}
	
	//******** setSpriteImg()*****************//
	//changes the icon to a particular image
	//for example: the front, the back
	public void setSpriteImg(){}
	
	public Sprite getSprite() {
		return this;
	}
	
	public String getSpriteImg() {
		return filename;
	}
}
